//
//  MXHomeWashVC.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/11.
//  Copyright (c) 2015年 霄 马. All rights reserved.
// 洗护选择页面
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "MXHomeWashVC.h"
#import "MXHomeWashCollectionViewCell.h"
#import "AFNetworkHandler.h"
#import "MXHomeWashModel.h"
#import "MXHomeWashItemDetailVC.h"
#import "MBProgressHUD.h"
@interface MXHomeWashVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *washKindArray;

@property (nonatomic, retain) NSNumber *washKindIDNum;

@property (nonatomic, retain) NSString *kindTitleString;

@property (nonatomic, retain) MBProgressHUD *HUD;

@property (nonatomic, retain) UIView *alterView;
@end

@implementation MXHomeWashVC
- (void)dealloc
{
    [_HUD release];
    [_alterView release];
    [_kindTitleString release];
    [_washKindIDNum release];
    [_washKindArray release];
    [_collectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //引入集视图
//    [self creatCollectionView];
    
    // 引入数据
    [self GetData];
    
    //初始化全局数组
    self.washKindArray = [NSMutableArray array];
    
    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHTPRO * 64)];
    titleLabel.backgroundColor = [UIColor whiteColor];
    titleLabel.text = @"";
    titleLabel.userInteractionEnabled = YES;
    [self.view addSubview:titleLabel];
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame = CGRectMake(WIDTHPRO * 10,HEIGHTPRO * 29,WIDTHPRO * 30,HEIGHTPRO * 30);
    [backButton setBackgroundImage:[UIImage imageNamed:@"backButton_blue"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
    [titleLabel addSubview:backButton];
    
    UILabel *titleWord = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 60, HEIGHTPRO * 24,WIDTHPRO * 120,HEIGHTPRO * 40)];
    titleWord.text = @"洗护专区";
    titleWord.textAlignment = NSTextAlignmentCenter;
    [titleWord setTextColor:[UIColor blueColor]];
    [titleLabel addSubview:titleWord];
    


    
}

-(void)uiActivityindicatorviewStart
{
    self.alterView =[[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)] autorelease];
    self.alterView.backgroundColor =[UIColor whiteColor];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.alterView animated:YES];
//    self.HUD.labelText = @"正在努力的给您加载";
    [self.HUD show:YES];
    
    [self.view addSubview:self.alterView];
    
}
-(void)uiActivityindicatorviewEnd
{
    [self.alterView removeFromSuperview];
}


#pragma mark - 创建集视图
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];

    flowLayout.itemSize = CGSizeMake(WIDTH / 2 , (HEIGHT - HEIGHTPRO * 64) / 3);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0 , 0 , 0, 0);
    
    self.collectionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(0,HEIGHTPRO * 64, WIDTH, HEIGHT  - HEIGHTPRO * 64) collectionViewLayout:flowLayout] autorelease];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    
    [self.collectionView registerClass:[MXHomeWashCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
    [self uiActivityindicatorviewStart];
}

/************************* 解析数据部分 **************************/
- (void)GetData
{
    NSString *washPath = @"http://api.nanyibang.com/daily-classify?daily_count=6";
    [AFNetworkHandler GETwithAFNByURL:washPath completion:^(id result) {
        NSArray *dataArr = [result objectForKey:@"data"];
        for (NSDictionary *washDict in dataArr) {
            MXHomeWashModel *model = [[[MXHomeWashModel alloc] initWithDic:washDict] autorelease];
            [self.washKindArray addObject:model];
        }
        [self creatCollectionView];
        [self uiActivityindicatorviewEnd];
    }];
}
#pragma mark - CollectionView 协议实现部分
/************************* CollectionView Delegate **************************/
//Collection中的组别个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个组别中元件的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.washKindArray.count;
}
//重用机制
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXHomeWashCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.layer.borderWidth = 0.25;
    cell.layer.borderColor = [UIColor grayColor].CGColor;
    [cell GetWashInfo:self.washKindArray[indexPath.row]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXHomeWashItemDetailVC *washDetailVC = [[[MXHomeWashItemDetailVC alloc] init] autorelease];
    washDetailVC.washID = [self.washKindArray[indexPath.row] kind_id];;
    washDetailVC.titleString = [self.washKindArray[indexPath.row] kind_name];
    [self presentViewController:washDetailVC animated:YES completion:^{
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/************************* Button 点击效果 **************************/
- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
